package org.example.diet_compass.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.example.diet_compass.domain.Goods;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;
import java.util.Map;

/**
* @author 86135
* @description 针对表【goods(商品项)】的数据库操作Mapper
* @createDate 2025-02-27 15:21:30
* @Entity generator.domain.Goods
*/
public interface GoodsMapper extends BaseMapper<Goods> {
    @Select("select * from   goods  order by average_score ")
    List<Goods> getRecommendsOrderByAVGScore();
    @Select("select * from   goods  where id = #{id}")
    Goods getGoodById(@Param("id") String id);

    @Update(" update goods g set g.average_score =\n" +
            "        (select avg(s.score) from `scores` s\n" +
            "        where s.goods_id = #{goodsId})" +
            "        where g.id=#{goodsId}")

    void updateAVGScore(String goodsId);


    @Select("SELECT ct.`name` AS topCategoryName,\n" +
            "       cc.`name` AS childCategoryName,\n" +
            "       ct.id     AS topCategoryId,\n" +
            "       cc.id     AS childCategoryId,\n" +
            "       g.*\n" +
            "FROM category_tops ct\n" +
            "         JOIN category_children cc\n" +
            "              ON ct.id = cc.top_category_id\n" +
            "         JOIN goods g ON cc.id = g.category_child_id")
    List<Map<String,Object>> getCategoryGoods();

    @Select("select price from goods where id=#{goodId}")
    Double getGoodPriceById(String goodId);

    @Select("<script>" +
            "SELECT * FROM goods WHERE id IN " +
            "<foreach item='id' collection='list' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    List<Goods> getGoodsByIds(@Param("list")List<String> goodIds);

    @Insert("<script> insert into goods values" +
            "<foreach collection = 'mapList' item='goodMap' separator = ','>" +
            "(#{goodMap.goodId},#{goodMap.name},null,#{goodMap.price},null,#{goodMap.picture},null,#{goodMap.category},0)" +
            "</foreach></script>")
    void insertGoodsBatch(@Param("mapList") List<Map<String,Object>> mapList);
}




